package com.supms.patrol.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.supms.patrol.domain.DailyInspectionEval;
import com.supms.patrol.domain.DailyInspectionItems;
import com.supms.patrol.domain.TourTestEval;
import com.supms.patrol.mapper.TourTestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.patrol.mapper.TourTestItemsMapper;
import com.supms.patrol.domain.TourTestItems;
import com.supms.patrol.service.ITourTestItemsService;

/**
 * 巡考内容项Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-03-15
 */
@Service
public class TourTestItemsServiceImpl implements ITourTestItemsService 
{
    @Autowired
    private TourTestItemsMapper tourTestItemsMapper;

    @Autowired
    private TourTestMapper tourTestMapper;

    /**
     * 查询巡考内容项
     * 
     * @param id 巡考内容项主键
     * @return 巡考内容项
     */
    @Override
    public TourTestItems selectTourTestItemsById(Long id)
    {
        return tourTestItemsMapper.selectTourTestItemsById(id);
    }

    /**
     * 查询巡考内容项列表
     * 
     * @param tourTestItems 巡考内容项
     * @return 巡考内容项
     */
    @Override
    public List<TourTestItems> selectTourTestItemsList(TourTestItems tourTestItems)
    {
        return tourTestItemsMapper.selectTourTestItemsList(tourTestItems);
    }

    /**
     * 新增巡考内容项
     * 
     * @param tourTestItems 巡考内容项
     * @return 结果
     */
    @Override
    public int insertTourTestItems(TourTestItems tourTestItems)
    {
        tourTestItems.setCreateTime(DateUtils.getNowDate());
        return tourTestItemsMapper.insertTourTestItems(tourTestItems);
    }

    /**
     * 修改巡考内容项
     * 
     * @param tourTestItems 巡考内容项
     * @return 结果
     */
    @Override
    public int updateTourTestItems(TourTestItems tourTestItems)
    {
        tourTestItems.setUpdateTime(DateUtils.getNowDate());
        return tourTestItemsMapper.updateTourTestItems(tourTestItems);
    }

    /**
     * 批量删除巡考内容项
     * 
     * @param ids 需要删除的巡考内容项主键
     * @return 结果
     */
    @Override
    public int deleteTourTestItemsByIds(Long[] ids)
    {
        return tourTestItemsMapper.deleteTourTestItemsByIds(ids);
    }

    /**
     * 删除巡考内容项信息
     * 
     * @param id 巡考内容项主键
     * @return 结果
     */
    @Override
    public int deleteTourTestItemsById(Long id)
    {
        return tourTestItemsMapper.deleteTourTestItemsById(id);
    }

    /**
     * 获取当前巡考表的具体情况记录
     *
     * @param tourTestId 巡考表ID
     * @return 巡考表和具体情况记录结果
     */
    @Override
    public List<TourTestItems> getTourTestItemByTTId(Long tourTestId) {
        return tourTestItemsMapper.getTourTestItemByTTId(tourTestId);
    }

    /**
     * 添加巡考具体情况记录
     *
     * @param tourTestItemsList 提交的巡考情况记录表
     * @return 结果
     */
    @Override
    public int insertTourTestEval(List<TourTestItems> tourTestItemsList) {
        List<TourTestEval> tourTestEvalList = new ArrayList<>();
        Long tourTestId = null;

        for (TourTestItems tourTestItems : tourTestItemsList) {
            TourTestEval tourTestEval = new TourTestEval();
            tourTestId = tourTestItems.getTourTestId();

            tourTestEval.setTourTestId(tourTestId);
            tourTestEval.setTourTestItemsId(tourTestItems.getId());
            tourTestEval.setSituation(tourTestItems.getMySituation());
            tourTestEval.setCreateTime(DateUtils.getNowDate());

            tourTestEvalList.add(tourTestEval);
        }

        // 将巡考记录标记改为已评价（"1"）
        tourTestMapper.updateTourTestEvalFlagById(tourTestId);
        return tourTestItemsMapper.insertTourTestEval(tourTestEvalList);
    }

    /**
     * 修改（更新）巡考具体情况记录
     *
     * @param tourTestItemsList 提交的巡考具体情况记录表
     * @return 结果
     */
    @Override
    public int updateTourTestEval(List<TourTestItems> tourTestItemsList) {
        // 批量更新数据效率太低，可以采用先删除后插入的方法更高效
        List<TourTestEval> tourTestEvalList = new ArrayList<>();
        Long tourTestId = null;

        for (TourTestItems tourTestItems : tourTestItemsList) {
            TourTestEval tourTestEval = new TourTestEval();
            tourTestId = tourTestItems.getTourTestId();

            tourTestEval.setTourTestId(tourTestId);
            tourTestEval.setTourTestItemsId(tourTestItems.getId());
            tourTestEval.setSituation(tourTestItems.getMySituation());
            tourTestEval.setCreateTime(DateUtils.getNowDate());

            tourTestEvalList.add(tourTestEval);
        }

        // 先删除巡考具体情况记录表原有的数据
        tourTestItemsMapper.deleteTourTestEvalByTourTestId(tourTestId);
        return tourTestItemsMapper.insertTourTestEval(tourTestEvalList);
    }
}
